﻿using System.Collections.Generic;
using System.Data;
using System.Linq;
using BO.Infrastructure.StaticData;
using BO.Infrastructure.Utils;
using DAL;
using Utilities.Entities;
using System;

namespace BO.Video
{
    public class VideoBO_NoCache
    {
        public static DataTable GetNewestVideo(int top, int zoneId)
        {
            DataTable dt;
            using (var db = new MainDB())
            {
                dt = db.StoredProcedures.GetNewestVideo(top, zoneId);
            }
            return DataFomatter.ReformatVideoTable(dt);
        }

        public static CategoryObject GetVideoZoneDetail(int zoneId)
        {
            var allVideoZones = VideoBO.GetAllVideoZones();
            return allVideoZones == null
                       ? null
                       : allVideoZones.FirstOrDefault(c => c.Id == zoneId);
        }

        public static List<CategoryObject> GetAllVideoZones()
        {
            DataTable dt;
            using (var db = new MainDB())
            {
                dt = db.StoredProcedures.GetAllVideoZones();
            }
            if (dt != null && dt.Rows.Count > 0)
            {
                var query = from DataRow r in dt.Rows
                            let id = PresentationUtils.Object2Integer(r[SqlConstants.ZoneVideo.Id].ToString())
                            select new CategoryObject
                            {
                                Id = id,
                                Name = r[SqlConstants.ZoneVideo.Name].ToString(),
                                DisplayUrl = r[SqlConstants.ZoneVideo.Url].ToString(),
                                Url = PresentationUtils.UrlBuilder.BuildZoneVideoUrl(id, r[SqlConstants.ZoneVideo.Url].ToString()),
                                ParentId = PresentationUtils.Object2Integer(r[SqlConstants.ZoneVideo.ParentId].ToString()
)
                            };
                return query.ToList();
            }
            return null;
        }

        public static DataTable GetVideoByMode(int top, int mode, int zoneId = 0)
        {
            DataTable dt;
            using (var db = new MainDB())
            {
                dt = db.StoredProcedures.GetVideoByMode(top, mode, zoneId);
            }
            return DataFomatter.ReformatVideoTable(dt);
        }

        public static DataTable GetListVideo(int pageIndex, int pageSize, int topExclusion, int zoneId)
        {
            DataTable dt;
            using (var db = new MainDB())
            {
                dt = db.StoredProcedures.GetListVideo(pageIndex, pageSize, topExclusion, zoneId);
            }
            return DataFomatter.ReformatVideoTable(dt);
        }

        public static DataTable GetMostViewVideo(int pageIndex, int pageSize, int topExclusion, int zoneId)
        {
            DataTable dt;
            using (var db = new MainDB())
            {
                dt = db.StoredProcedures.GetMostViewVideo(pageIndex, pageSize, topExclusion, zoneId);
            }
            return DataFomatter.ReformatVideoTable(dt);
        }

        public static int CountVideoByZone(int zoneId, int topExclusion)
        {
            DataTable dt;
            using (var db = new MainDB())
            {
                dt = db.StoredProcedures.CountVideoByZone(zoneId, topExclusion);
            }
            if (dt != null && dt.Rows.Count > 0)
            {
                int n;
                int.TryParse(dt.Rows[0][0].ToString(), out n);
                return n;
            }
            return default(int);
        }

        internal static DataTable GetVideoDetail(int videoId)
        {
            DataTable dt;
            using (var db = new MainDB())
            {
                dt = db.StoredProcedures.GetVideoDetail(videoId);
            }
            return DataFomatter.ReformatVideoTable(dt);
        }

        public static DataTable GetVideoByTime(DateTime startDate, DateTime endDate)
        {
            DataTable dt;
            using (var db = new MainDB())
            {
                dt = db.StoredProcedures.GetVideoByTime(startDate, endDate);
            }
            return DataFomatter.ReformatVideoTable(dt);
        }

        internal static DataTable GetEmbedVideo(int top, int type, int zoneId = 0)
        {
            DataTable dt;
            using (var db = new MainDB())
            {
                dt = db.StoredProcedures.GetEmbedVideo(top, type, zoneId);
            }
            return DataFomatter.ReformatVideoTable(dt);
        }
    }
}
